cmake_minimum_required(VERSION 3.5.1)

project(Remote_Calculate C CXX)

include(cmake/common.cmake)

# 获取 proto 文件的绝对路径
get_filename_component(rc_proto "../protos/remote_calculate.proto" ABSOLUTE)

# 获取 proto 文件所在目录的旧别名
get_filename_component(rc_proto_path "${rc_proto}" PATH)

# 被生成的代码所在路径 build 文件夹下
set (rc_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/remote_calculate.pb.cc")
set (rc_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/remote_calculate.pb.h")
set (rc_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/remote_calculate.grpc.pb.cc")
set (rc_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/remote_calculate.grpc.pb.h")

# 生成指定 OUTPUT 文件
add_custom_command(
    OUTPUT "${rc_proto_srcs}" "${rc_proto_hdrs}" "${rc_grpc_srcs}" "${rc_grpc_hdrs}"
    COMMAND ${_PROTOBUF_PROTOC}
    ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
    --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
    -I "${rc_proto_path}"
    --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
    "${rc_proto}"
    DEPENDS "${rc_proto}"
)

# 项目 include 目录
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

# 导出静态库
add_library(rc_grpc_proto
    ${rc_grpc_srcs}
    ${rc_grpc_hdrs}
    ${rc_proto_srcs}
    ${rc_proto_hdrs}
)

# 将其它静态库链接到 rc_grpc_proto 库
target_link_libraries(rc_grpc_proto
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF}
)

# 生成可执行文件
foreach(_target
    remote_calculate_server remote_calculate_client
)
add_executable(${_target} "${_target}.cc")
target_link_libraries(${_target}
    rc_grpc_proto
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF}
)
endforeach()




